$(function () {

    var roomid="";
    var token="";
    var  teacherCode ="";
    var  ptcode ="";


    $("body").on('click','.shelves',function () {
        //下架
        var id = $(this).data('id');
        updateEntity(id,2);
    }).on('click','.theShelves',function () {
        //上架
        var id = $(this).data('id');
        updateEntity(id,3);
    }).on('click','.del',function () {
        var id = $(this).data('id');
        updateEntity(id,4);
    }).on('click','#onBatch',function () {
        var ids = obtainIds(2);
        if(ids!=null){
            updateEntity(ids,2);
        }
    }).on('click','#offBatch',function () {
        var ids = obtainIds(3);
        if(ids!=null){
            updateEntity(ids,3);
        }
    }).on('click','#deleteBatch',function () {//批量删除
        var ids = obtainIds(4);
        if(ids!=null){
            updateEntity(ids,4);
        }
    }).on('click','#btn1,#btn2',function () {
        getData(1);
    }).on('click','.opneInFo',function(){
         var id =   $(this).data("id");
         window.location.href = baseloaction+"/admin/IntegralGood/integralRecord?id="+id;
    });
    getData(1);
    /**
     * 获取免费直播数据列表
     * @param pageNo
     */
    function getData(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/IntegralGood/ajaxListPage',//请求URL
            contextId: 'courseOpen-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: getData, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'select-all-courseOpen', //全选框ID
            callback: function () {//数据加载完成回调
                $("#count").html('(共'+total+'个商品)');
            }
        };
        ajaxGetListData(obj);
    }

    /**
     * 修改状态
     * @param ids
     * @param status
     */
    function updateEntity(ids,status) {
        var message ="";
        if(status==2){
            message = "确认要下架选中的数据？";
        }
        if(status==3){
            message = "确认要上架选中的数据？";
        }
        if(status ==4){
            message = "确认要删除选中的数据？";
        }
        icModale(2, message, 3, function () {
            $.ajax({
                url:baseloaction +'/admin/IntegralGood/updateStatus',
                type:'post',
                dataType:'json',
                data:{'ids':ids,'status':status},
                success:function (result) {
                    if(result.success==true){
                        var arr = (ids+'').split(",");
                        $.each(arr,function (index,id) {
                      var that =     $("#status-action-"+id);
                         var statusTo =   $("#status-"+id);
                            if(status==2) {
                                that.removeClass("shelves");
                                that.addClass("theShelves");
                                that.text("上架");
                                statusTo.html("  <span  class=\"label label-danger\">已下架</span>");

                            }
                            if(status==3){//
                                that.removeClass("theShelves");
                                that.addClass("shelves");
                                that.text("下架");
                                statusTo.html(" <span class=\"label label-info\">已上架</span>");
                            }
                            if(status==4){//删除
                                $("#entity-open-"+id).remove();
                            }
                        });
                        icModale(0,result.message);
                    }else{
                        icModale(0,result.message);
                    }
                },
                error:function (error) {
                    icModale(0,'系统错误，操作失败');
                }
            });
        });
    }

    /**
     * 获取勾选ID是否为空
     * @param obj
     * @return {*}
     */
    function obtainIds(status) {
        var obj = $('input[name="courseOpenId"]:checked');
        if(obj.length <= 0){
            if(status==2){
                icModale(0,"请选择要上架的数据");
            }
            if(status==3){
                icModale(0,"请选择要下架的数据");
            }
            if(status==4){
                icModale(0,"请选择要删除的数据");
            }
            return null;
        }else {
            var courseOpenIds = obj.map(function () {
                return this.value;
            }).get().join(",");
            return courseOpenIds;
        }
    }
});
